package com.fwtai.quartz;

import com.fwtai.service.SpringContextHolder;
import com.fwtai.web.TaskHandlerDao;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@EnableScheduling
public class MallDeleteOrder implements Job{

  public final static String groupName = MallDeleteOrder.class.getName();//用于删除延时任务

  public final static String dateKey = "taskKid";//数据key的标识

  @Override
  public void execute(final JobExecutionContext context){
    final JobDetail jobDetail = context.getJobDetail();
    final JobDataMap jobDataMap = jobDetail.getJobDataMap();
    final long out_trade_no = jobDataMap.getLong(dateKey);
    final TaskHandlerDao taskHandlerDao = SpringContextHolder.getBean(TaskHandlerDao.class);
    final Long kid = taskHandlerDao.getMallOrderStatus(out_trade_no);//查询是否为未支付的订单id,此行可以不用,但此方法比较保险
    if(kid != null){
      final List<Long> list = new ArrayList<>(1);
      list.add(kid);
      final int rows = taskHandlerDao.delTimeoutMallOrder(list);
      if(rows > 0){
        ToolQuartz.deleteJob(String.valueOf(out_trade_no),groupName);
      }
    }
  }
}